<?php
namespace Api\Controller;

class DoctorController extends BaseController
{
    function bindPatient()
    {
        $_POST['doctor_id'] = is_login();
        $model = D('DoctorUser');
        if ($model->create()) {
            if ($model->add()) {
                $re['status'] = 1;
                $re['data'] = '添加成功！';
            } else {
                $re['status'] = 0;
                $re['msg'] = $model->getError();
            }
        } else {
            $re['status'] = 0;
            $re['msg'] = $model->getError();
        }
        $this->ajaxReturn($re);
    }

    function patientList()
    {
        $map['doctor_id'] = is_login();
        $model = D('DoctorUser');
        if ($data = $model->where($map)->select()) {
            $re['status'] = 1;
            $re['data'] = $data;
        } else {
            $re['status'] = 0;
            $re['msg'] = $model->getError();
        }
        $this->ajaxReturn($re);
    }

    function applyAgent()
    {
        $_POST['doctor_id'] = is_login();
        $model = D('DoctorApplyAgent');
        $map['doctor_id'] = is_login();
        if($model->where($map)->find()){
            $re['status'] = 0;
            $re['msg'] = '您已经申请过设备代理，等客服人员联系您！';
        }
        if ($model->create()) {
            if ($model->add()) {
                $re['status'] = 1;
                $re['data'] = '申请发送成功，等系统工作人员联系你！';
            } else {
                $re['status'] = 0;
                $re['msg'] = $model->getError();
            }
        } else {
            $re['status'] = 0;
            $re['msg'] = $model->getError();
        }
        $this->ajaxReturn($re);
    }

    function applyConsultation()
    {
        $_POST['uid'] = is_login();
        $_POST['type'] = 11;
        $model = D('ApplyConsultation');
        if ($model->create()) {
            if ($model->add()) {
                $re['status'] = 1;
                $re['data'] = '申请发送成功，等系统工作人员联系你！';
            } else {
                $re['status'] = 0;
                $re['msg'] = $model->getError();
            }
        } else {
            $re['status'] = 0;
            $re['msg'] = $model->getError();
        }
        $this->ajaxReturn($re);
    }

    //获取绑定患者信息
    function getPatientInfo()
    {
        $map['doctor_id'] = is_login();
        $uid = I('post.user_id');
        $map['user_id'] = $uid;
        $model = M('DoctorUser');
        if ($info = $model->where($map)->find()) {
            $model2 = M('Member');
            $map2['uid'] = $uid;
            $info2 = $model2->where($map2)->find();
            $model3 = M('UcenterMember');
            $map3['id'] = $uid;
            $info3 = $model3->where($map3)->find();
            $info = array_merge($info2, $info3, $info);
            $re['status'] = 1;
            $re['data'] = $info;

        } else {
            $re['status'] = 0;
            $re['msg'] = '获取信息失败！';
        }
        $this->ajaxReturn($re);
    }

    function questionList()
    {
        $map['doctor_id'] = is_login();
        $map['pid'] = 0;
        $map['status'] = 0;
        $model = M('question');
        $rs = $model->where($map)->order('id desc')->select();
        if ($rs || $rs == null) {
            $re['status'] = 1;
            $re['data'] = $rs;
        } else {
            $re['status'] = 0;
            $re['msg'] = $model->getError();
        }
        $this->ajaxReturn($re);
    }

    //医生未回复
    function questionNoReply()
    {
        $model = D('question');
        $map['doctor_id'] = is_login();
        $map['pid'] = 0;
        $map['status'] = 0;
        $rs = $model->where($map)->order('status asc, id desc')->select();
        if ($rs || $rs == null) {
            $re['status'] = 1;
            $re['data'] = $rs;
        } else {
            $re['status'] = 0;
            $re['data'] = $model->getError();
        }
        $this->ajaxReturn($re);
    }

    //医生未回复数量显示
    function questionAbout()
    {
        $model = D('question');
        $map['doctor_id'] = is_login();
        $map['pid'] = 0;
        $map['status'] = 0;
        if ($rs = $model->where($map)->count()) {
            $re['status'] = 1;
            $re['data'] = $rs;
        } else {
            $re['status'] = 0;
            $re['data'] = $model->getError();
        }
        $this->ajaxReturn($re);
    }
    //待补充
    function questionLastid(){

    }

    //医生已回复
    function questionHasReply()
    {
        $model = D('question');
        $map['doctor_id'] = is_login();
        $map['pid'] = 0;
        $map['status'] = array('gt', 1);
        $rs = $model->where($map)->order('status desc, id desc')->select();
        if ($rs || $rs == null) {
            $re['status'] = 1;
            $re['data'] = $rs;
        } else {
            $re['status'] = 0;
            $re['data'] = $model->getError();
        }
        $this->ajaxReturn($re);
    }

    //医生已回复
    function questionEnd()
    {
        $model = D('question');
        $map['doctor_id'] = is_login();
        $map['pid'] = 0;
        $map['status'] = 1;
        if ($rs = $model->where($map)->order('id desc')->select()) {
            $re['status'] = 1;
            $re['data'] = $rs;
        } else {
            $re['status'] = 0;
            $re['data'] = $model->getError();
        }
        $this->ajaxReturn($re);
    }

    //获取与问题相关的回答以及再提问。。。。
    function getOneQuestion()
    {
        $model = D('question');
        $map['topid'] = I('post.id');
        $map['doctor_id'] = is_login();
        if ($rs = $model->where($map)->select()) {
            $re['status'] = 1;
            $re['data'] = $rs;
        } else {
            $re['status'] = 0;
            $re['data'] = $model->getError();
        }
        $this->ajaxReturn($re);
    }

    //回复问题
    function replyQuestion()
    {
        $uid = is_login();
        $id = I('post.id');
        unset($_POST['id']);
        $_POST['inituid'] = I('post.inituid');
        $_POST['username'] = get_nickname($_POST['uid']);
        $_POST['uid'] = $uid;
        $_POST['doctor_id'] = $uid;
        $_POST['pid'] = $id;
        $_POST['topid'] = $id;
        $model = D('question');
        if ($model->create()) {
            if ($rs = $model->add()) {
                $map['doctor_id'] = is_login();
                $map['id'] = $id;
                $data['status'] = 3;
                if ($model->where($map)->save($data) !== false) {
                    $re['status'] = 1;
                    $re['data'] = $rs;
                } else {
                    $re['status'] = 0;
                    $re['addon'] = 3;
                    $re['msg'] = $model->getError();
                    $model->delete($rs);
                }
            } else {
                $re['status'] = 0;
                $re['addon'] = 2;
                $re['msg'] = $model->getError();
            }
        } else {
            $re['status'] = 0;
            $re['msg'] = $model->getError();
        }
        $this->ajaxReturn($re);
    }


    //yisheng获取consultationList
    function consultationList()
    {
        $map['doctor_id'] = is_login();
        $map['status'] = 1;
        $model = M('ApplyConsultation');
        $rs = $model->where($map)->order('id desc')->select();
        if ($rs || $rs == null ) {
            $re['status'] = 1;
            $re['data'] = $rs;
        } else {
            $re['status'] = 0;
            $re['data'] = $model->getError();
        }
        $this->ajaxReturn($re);
    }

    //getConsultation 获取为常见order的申请会诊信息
    function getConsultation()
    {
        $map['doctor_id'] = is_login();
        $map['id'] = I('post.id');
        $model = M("ApplyConsultation");
        if ($rs = $model->where($map)->find()) {
            $re['status'] = 1;
            $re['data'] = $rs;
        } else {
            $re['status'] = 0;
            $re['data'] = $model->getError();
        }
        $this->ajaxReturn($re);
    }

    //createOrder 建立订单
    function createOrder()
    {
//		$status = M('ApplyConsultation')->getFieldById(I('post.id'), 'status');
//		if($status == 1){
//			$re['status'] = 0;
//			$re['msg'] = '已经建立过此申请对应订单！';
//			$this->ajaxReturn($re);
//		}
//        var_dump($_POST);
        $_POST['status'] = 0;
        $_POST['add_time'] = time();
        $_POST['doctor_id'] = is_login();
        $model = D('ConsultationOrder');
        if ($model->create()) {
            if ($model->add()) {
//                $re['ss'] = $model->getLastSql();
                $data['id'] = I('post.id');
                $data['status'] = 0;//已处理的订单为0待确认的订单为1
                M('ApplyConsultation')->save($data);
                $re['status'] = 1;
                $re['data'] = '创建订单成功等待用户付款';
            } else {
                $re['status'] = 0;
                $re['msg'] = $model->getError();
            }
        } else {
            $re['status'] = 0;
            $re['msg'] = $model->getError();
        }
        $this->ajaxReturn($re);
    }

    //获取已建立未付款订单
    function consultationOrderList()
    {
        $map['doctor_id'] = is_login();
        $map['status'] = 0;
        $model = D('ConsultationOrder');
        if ($rs = $model->where($map)->select()) {
            $re['status'] = 1;
            $re['data'] = $rs;
        } else {
            $re['status'] = 0;
            $re['data'] = $model->getError();
        }
        $this->ajaxReturn($re);
    }

    /**
     * 医生端开关配置
     */
    public function config()
    {
        $uid = is_login();
        $tuwen_open = I('post.tuwen_open', 0, 'intval');
        $tuwen_price = I('post.tuwen_price', 0, 'intval');
        $yuyin_open = I('post.yuyin_open', 0, 'intval');
        $yuyin_price = I('post.yuyin_price', 0, 'intval');
        $dianhua_open = I('post.dianhua_open', 0, 'intval');
        $dianhua_price = I('post.dianhua_price', 0, 'intval');
        $private_open = I('post.private_open', 0, 'intval');
        $private_price = I('post.private_price', 0, 'intval');

        $info = M('DoctorConfig')->find($uid);

        $data['tuwen_open'] = $tuwen_open;
        $data['tuwen_price'] = $tuwen_price;
        $data['yuyin_open'] = $yuyin_open;
        $data['yuyin_price'] = $yuyin_price;
        $data['dianhua_open'] = $dianhua_open;
        $data['dianhua_price'] = $dianhua_price;
        $data['private_open'] = $private_open;
        $data['private_price'] = $private_price;
        $data['uid'] = $uid;
        $model = M('DoctorConfig');
        if (empty($info)) {
            $result = $model->add($data);
//			var_dump($model->getLastSql());
            if ($result)
                $this->success('添加成功,id为' . $result, 'refresh');
            else
                $this->error("出错了" . $model->getError());
        } else {
            $result = $model->save($data);
            if ($result >= 0)
                $this->success('受影响的行数为' . $result, 'refresh');
            else
                $this->error("出错了" . $model->getError());
        }


    }

    /**
     * 获取配置
     */
    public function getConfig(){
        $id=is_login();
        $data=M('DoctorConfig')->find($uid);
        if(empty($data)) {
            $data['uid']=$id;
            $data['tuwen_open'] = 0;
            $data['tuwen_price'] = 0;
            $data['yuyin_open'] = 0;
            $data['yuyin_price'] = 0;
            $data['dianhua_open'] = 0;
            $data['dianhua_price'] = 0;
            $data['private_open'] = 0;
            $data['private_price'] = 0;
        }

        $this->success('操作成功','refresh',$data);
    }
    /**
     * 写诊断记录
     */
    public function writerLog()
    {
        $did = is_login();
        $uid = I('post.user_id', 0, 'intval');
        if (empty($did)) {
            $this->error('用户id获取失败!');
        }
        $content = I('post.content', '', 'op_t');
        if (empty($content)) {
            $this->error('记录内容不能为空');
        }
        $order_id = I('post.order_id', 0, 'op_t');
        $happen_time = I('post.happen_time', date('Y-m-d H:i:s'), 'op_t');
        $modle = M('DoctorLog');
        $data['user_id'] = $uid;
        if ($order_id)
            $data['order_id'] = $order_id;
        $data['happen_time'] = $happen_time;
        $data['content'] = $content;
        $data['doctor_id'] = $did;
        $result = $modle->add($data);
//        var_dump($modle->getLastSql());
        if ($result)
            $this->success('添加成功,id为' . $result, 'refresh');
        else
            $this->error('操作失败,' . $modle->getError());
    }

    /**
     * 查看诊断记录
     */
    public function getLog()
    {
        $did = is_login();
        $id = I('post.id', 0, 'intval');
        $uid = I('post.user_id', 0, 'intval');
        if (empty($uid) && empty($id))
            $this->error('病人id获取错误或者没有指定记录id!');
        if ($id)
            $where['id'] = $id;
        if ($uid)
            $where['user_id'] = $did;
        $model = M('DoctorLog');

        $perPage = I('post.perPage', 5, 'intval');
        $page = I('post.page', 1, 'intval');
        $maxPage = $count / $perPage;

        if ($page != 1 && $page > $maxPage)
            $this->error('没有此页面');
        $where['doctor_id']=$did;
        $count = $model->where($where)->count();
        $list = $model->where($where)->page($page, $perPage)->select();
//        var_dump($model->getLastSql());
        if (empty($list))
            $this->error('没有数据!');
        else
            $this->success('获取成功', 'refresh', array('count' => $count, 'list' => $list));
    }
}

?>